package com.my.service.imp;

import com.my.dao.AddressDao;
import com.my.pojo.Address;
import com.my.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    AddressDao addressDao;

    @Autowired
    public void setAddressDao(AddressDao addressDao) {
        this.addressDao = addressDao;
    }

    @Override
    public List<Address> showAddress(Integer userId) {
        return addressDao.findAll(
                (root, cq, cb) -> cb.equal(root.get("user"), userId)
        );
    }

    @Override
    public Integer addAddress(Address address) {
        return addressDao.save(address) == null ? -1 : 0;
    }

    @Override
    public Integer deleteAddress(Integer addressId, Integer userId) {
        Address address = addressDao.findOne(addressId);
        if (address == null || !userId.equals(address.getUser().getUserId())) {
            return -1;
        }
        address.setStatus(-1);
        return addressDao.save(address) == null ? -2 : 0;
    }

    @Override
    public Address selById(Integer addressId) {
        return this.addressDao.findOne(addressId);
    }


}
